var billHistory = {
	bill : null,
	printData : function() {
		window.printBill(billHistory.bill, function(result) {
			// 不加延迟会有问题.问题还未知
			window.setTimeout(app.unmask, 50);
			if (result) {
				if (result.success == true) {
					alert(result.msg);
				} else {
					alert(result.msg);
				}
			} else {
				alert('已经调用打印功能,但没有返回消息');
			}
		});
	},
	print : function() {
		app.mask();
		window.setTimeout(billHistory.printData, 50);

	},
	show : function(transtion) {
		var db = app.getDB();
		var sql = "select send_time as sendTime,total_price as totalPrice,id,table_name as tableName,print_status as status from bill order by send_time desc limit 0,10"
		db.transaction(function(tx) {
			tx.executeSql(sql, [], function(tx, results) {
				var len = results.rows.length;
				var template = $.createTemplateURL('templates/bill_history.html');
				var data = [];
				for (var i = 0; i < len; i++) {
					data.push(results.rows.item(i));
				}
				var html = template.get(data);
				app.loadHTML(html, {
					transition : transtion
				})

			}, errorCB);

		}, errorCB);
	},
	showDetail : function(billId) {
		var db = app.getDB();

		db.transaction(function(tx) {

			tx.executeSql("select * from bill where id =?", [billId], function(
					tx, results) {
				if (results.rows.length < 1) {
					alert("未找到指定的定单");
					return;
				}
				var rowData = results.rows.item(0);
				var tmpBill = new Bill();
				tmpBill.setSummary(rowData.id,rowData.table_name,rowData.total_price,rowData.send_time,rowData.print_time,rowData.print_status);
				var sql = "select * from bill_menu where bill_id =?";
				tx.executeSql(sql, [rowData.id], function(tx, billMenus) {
					var len = billMenus.rows.length;
					for (var i = 0; i < len; i++) {
						tmpBill.addBillMenuWithCount(billMenus.rows.item(i))
					}
					billHistory.bill = tmpBill;
					jsTouch.overlayTemplate('templates/bill_history_detail.html', tmpBill, {
						modal : false,
						width : 2000,
						height : 2000
					});

				}, function(e) {
					console.log(e);
				});

			}, function(e) {
				console.log(e);
			});

		}, function(e) {
			console.log(e);
		});
	},
	readMore : function() {
		var start = $('#bill_history_table_id tr').length;
		var sql = "select send_time as sendTime,total_price as totalPrice,id,table_name as tableName,print_status as status from bill order by send_time desc limit ?,10"
		var db = app.getDB();
		db.transaction(function(tx) {
			tx.executeSql(sql, [start], function(tx, results) {
				var len = results.rows.length;
				var template = $.createTemplateURL('templates/bill_history_table_tr.html');
				var data = [];
				for (var i = 0; i < len; i++) {
					data.push(results.rows.item(i));
				}
				var html = template.get(data);
				$('#bill_history_table_id').append(html);
				var currentBox = jsTouch.getCurrentBox($('div.content')[0]);
				currentBox.scroll._checkDOMChanges();
			}, errorCB);

		}, errorCB);
	}

};